<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - SimpleHire</title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        .register-container {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 2rem 0;
        }
        
        .register-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            border: none;
            overflow: hidden;
        }
        
        .register-header {
            background: linear-gradient(135deg, #059669, #047857);
            color: white;
            padding: 2rem;
            text-align: center;
        }
        
        .register-body {
            padding: 2.5rem;
        }
        
        .form-control {
            border-radius: 10px;
            border: 2px solid #e5e7eb;
            padding: 0.75rem 1rem;
            font-size: 1rem;
            transition: all 0.3s ease;
        }
        
        .form-control:focus {
            border-color: #059669;
            box-shadow: 0 0 0 0.2rem rgba(5, 150, 105, 0.25);
        }
        
        .form-control.is-valid {
            border-color: #059669;
        }
        
        .form-control.is-invalid {
            border-color: #dc2626;
        }
        
        .btn-register {
            background: linear-gradient(135deg, #059669, #047857);
            border: none;
            border-radius: 10px;
            padding: 0.75rem 2rem;
            font-weight: 600;
            font-size: 1.1rem;
            transition: all 0.3s ease;
        }
        
        .btn-register:hover {
            background: linear-gradient(135deg, #047857, #065f46);
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(5, 150, 105, 0.3);
        }
        
        .form-floating {
            margin-bottom: 1.5rem;
        }
        
        .alert {
            border-radius: 10px;
            border: none;
        }
        
        .divider {
            text-align: center;
            margin: 2rem 0;
            position: relative;
        }
        
        .divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: #e5e7eb;
        }
        
        .divider span {
            background: white;
            padding: 0 1rem;
            color: #6b7280;
            font-size: 0.9rem;
        }
        
        .login-link {
            text-align: center;
            margin-top: 1.5rem;
        }
        
        .login-link a {
            color: #059669;
            text-decoration: none;
            font-weight: 500;
        }
        
        .login-link a:hover {
            text-decoration: underline;
        }
        
        .brand-logo {
            font-size: 3rem;
            margin-bottom: 1rem;
        }
        
        .password-strength {
            margin-top: 0.5rem;
        }
        
        .strength-bar {
            height: 4px;
            border-radius: 2px;
            background: #e5e7eb;
            overflow: hidden;
        }
        
        .strength-fill {
            height: 100%;
            transition: all 0.3s ease;
            width: 0%;
        }
        
        .strength-weak { background: #dc2626; }
        .strength-fair { background: #d97706; }
        .strength-good { background: #059669; }
        .strength-strong { background: #047857; }
        
        .validation-feedback {
            font-size: 0.875rem;
            margin-top: 0.25rem;
        }
        
        .check-result {
            font-size: 0.875rem;
            margin-top: 0.25rem;
        }
        
        .check-result.available {
            color: #059669;
        }
        
        .check-result.unavailable {
            color: #dc2626;
        }
        
        .check-result.checking {
            color: #6b7280;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-8 col-lg-6">
                    <div class="card register-card">
                        <div class="register-header">
                            <div class="brand-logo">
                                <i class="bi bi-person-plus"></i>
                            </div>
                            <h2 class="mb-0">加入 SimpleHire</h2>
                            <p class="mb-0 mt-2 opacity-75">开始您的AI面试之旅</p>
                        </div>
                        
                        <div class="register-body">
                            <!-- Error Messages -->
                            <div class="alert alert-danger" role="alert" th:if="${error}">
                                <i class="bi bi-exclamation-triangle me-2"></i>
                                <span th:text="${error}"></span>
                            </div>
                            
                            <!-- Registration Form -->
                            <form th:action="@{/register}" method="post" class="needs-validation" novalidate>
                                <div class="form-floating">
                                    <input type="text" 
                                           class="form-control" 
                                           id="fullName" 
                                           name="fullName" 
                                           placeholder="姓名"
                                           required
                                           th:value="${user != null ? user.fullName : ''}">
                                    <label for="fullName">
                                        <i class="bi bi-person me-2"></i>姓名
                                    </label>
                                    <div class="invalid-feedback">
                                        请输入您的姓名
                                    </div>
                                </div>
                                
                                <div class="form-floating">
                                    <input type="text" 
                                           class="form-control" 
                                           id="username" 
                                           name="username" 
                                           placeholder="用户名"
                                           required
                                           pattern="[a-zA-Z0-9_]{3,20}"
                                           th:value="${user != null ? user.username : ''}">
                                    <label for="username">
                                        <i class="bi bi-at me-2"></i>用户名
                                    </label>
                                    <div class="invalid-feedback">
                                        用户名必须为3-20位字母、数字或下划线
                                    </div>
                                    <div class="check-result" id="username-check"></div>
                                </div>
                                
                                <div class="form-floating">
                                    <input type="email" 
                                           class="form-control" 
                                           id="email" 
                                           name="email" 
                                           placeholder="邮箱"
                                           required
                                           th:value="${user != null ? user.email : ''}">
                                    <label for="email">
                                        <i class="bi bi-envelope me-2"></i>邮箱
                                    </label>
                                    <div class="invalid-feedback">
                                        请输入有效的邮箱地址
                                    </div>
                                    <div class="check-result" id="email-check"></div>
                                </div>
                                
                                <div class="form-floating">
                                    <input type="password" 
                                           class="form-control" 
                                           id="password" 
                                           name="password" 
                                           placeholder="密码"
                                           required
                                           minlength="6">
                                    <label for="password">
                                        <i class="bi bi-lock me-2"></i>密码
                                    </label>
                                    <div class="invalid-feedback">
                                        密码至少需要6位字符
                                    </div>
                                    <div class="password-strength">
                                        <div class="strength-bar">
                                            <div class="strength-fill" id="strength-fill"></div>
                                        </div>
                                        <small class="text-muted" id="strength-text">请输入密码</small>
                                    </div>
                                </div>
                                
                                <div class="form-floating">
                                    <input type="password" 
                                           class="form-control" 
                                           id="confirmPassword" 
                                           name="confirmPassword" 
                                           placeholder="确认密码"
                                           required>
                                    <label for="confirmPassword">
                                        <i class="bi bi-lock-fill me-2"></i>确认密码
                                    </label>
                                    <div class="invalid-feedback">
                                        两次输入的密码不一致
                                    </div>
                                </div>
                                
                                <div class="d-grid">
                                    <button type="submit" class="btn btn-success btn-register">
                                        <i class="bi bi-person-plus me-2"></i>
                                        注册账户
                                    </button>
                                </div>
                            </form>
                            
                            <div class="divider">
                                <span>已有账户？</span>
                            </div>
                            
                            <div class="d-grid">
                                <a href="/login" class="btn btn-outline-success">
                                    <i class="bi bi-box-arrow-in-right me-2"></i>
                                    立即登录
                                </a>
                            </div>
                            
                            <div class="login-link">
                                <a href="/">
                                    <i class="bi bi-house me-1"></i>
                                    返回首页
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
        // Password strength checker
        function checkPasswordStrength(password) {
            let strength = 0;
            let text = '';
            let className = '';
            
            if (password.length >= 6) strength += 1;
            if (password.length >= 8) strength += 1;
            if (/[a-z]/.test(password)) strength += 1;
            if (/[A-Z]/.test(password)) strength += 1;
            if (/[0-9]/.test(password)) strength += 1;
            if (/[^A-Za-z0-9]/.test(password)) strength += 1;
            
            const strengthFill = document.getElementById('strength-fill');
            const strengthText = document.getElementById('strength-text');
            
            if (password.length === 0) {
                strengthFill.style.width = '0%';
                strengthFill.className = 'strength-fill';
                strengthText.textContent = '请输入密码';
                return;
            }
            
            if (strength <= 2) {
                strengthFill.style.width = '25%';
                strengthFill.className = 'strength-fill strength-weak';
                strengthText.textContent = '密码强度：弱';
            } else if (strength <= 3) {
                strengthFill.style.width = '50%';
                strengthFill.className = 'strength-fill strength-fair';
                strengthText.textContent = '密码强度：一般';
            } else if (strength <= 4) {
                strengthFill.style.width = '75%';
                strengthFill.className = 'strength-fill strength-good';
                strengthText.textContent = '密码强度：良好';
            } else {
                strengthFill.style.width = '100%';
                strengthFill.className = 'strength-fill strength-strong';
                strengthText.textContent = '密码强度：强';
            }
        }
        
        // Username availability checker
        let usernameTimeout;
        function checkUsernameAvailability(username) {
            const checkResult = document.getElementById('username-check');
            
            if (username.length < 3) {
                checkResult.textContent = '';
                return;
            }
            
            checkResult.textContent = '检查中...';
            checkResult.className = 'check-result checking';
            
            clearTimeout(usernameTimeout);
            usernameTimeout = setTimeout(() => {
                fetch(`/api/check-username?username=${encodeURIComponent(username)}`)
                    .then(response => response.json())
                    .then(data => {
                        if (data.available) {
                            checkResult.textContent = '✓ 用户名可用';
                            checkResult.className = 'check-result available';
                        } else {
                            checkResult.textContent = '✗ 用户名已被使用';
                            checkResult.className = 'check-result unavailable';
                        }
                    })
                    .catch(() => {
                        checkResult.textContent = '';
                    });
            }, 500);
        }
        
        // Email availability checker
        let emailTimeout;
        function checkEmailAvailability(email) {
            const checkResult = document.getElementById('email-check');
            
            if (!email.includes('@')) {
                checkResult.textContent = '';
                return;
            }
            
            checkResult.textContent = '检查中...';
            checkResult.className = 'check-result checking';
            
            clearTimeout(emailTimeout);
            emailTimeout = setTimeout(() => {
                fetch(`/api/check-email?email=${encodeURIComponent(email)}`)
                    .then(response => response.json())
                    .then(data => {
                        if (data.available) {
                            checkResult.textContent = '✓ 邮箱可用';
                            checkResult.className = 'check-result available';
                        } else {
                            checkResult.textContent = '✗ 邮箱已被使用';
                            checkResult.className = 'check-result unavailable';
                        }
                    })
                    .catch(() => {
                        checkResult.textContent = '';
                    });
            }, 500);
        }
        
        // Password confirmation checker
        function checkPasswordConfirmation() {
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword');
            
            if (confirmPassword.value && password !== confirmPassword.value) {
                confirmPassword.setCustomValidity('两次输入的密码不一致');
            } else {
                confirmPassword.setCustomValidity('');
            }
        }
        
        // Event listeners
        document.addEventListener('DOMContentLoaded', function() {
            const passwordField = document.getElementById('password');
            const confirmPasswordField = document.getElementById('confirmPassword');
            const usernameField = document.getElementById('username');
            const emailField = document.getElementById('email');
            
            passwordField.addEventListener('input', function() {
                checkPasswordStrength(this.value);
                checkPasswordConfirmation();
            });
            
            confirmPasswordField.addEventListener('input', checkPasswordConfirmation);
            
            usernameField.addEventListener('input', function() {
                checkUsernameAvailability(this.value);
            });
            
            emailField.addEventListener('input', function() {
                checkEmailAvailability(this.value);
            });
            
            // Auto-focus on full name field
            document.getElementById('fullName').focus();
        });
        
        // Form validation
        (function() {
            'use strict';
            window.addEventListener('load', function() {
                var forms = document.getElementsByClassName('needs-validation');
                var validation = Array.prototype.filter.call(forms, function(form) {
                    form.addEventListener('submit', function(event) {
                        if (form.checkValidity() === false) {
                            event.preventDefault();
                            event.stopPropagation();
                        }
                        form.classList.add('was-validated');
                    }, false);
                });
            }, false);
        })();
    </script>
</body>
</html>